<template>
  <div class="equipment-add">
    <div class="shade"></div>
    <div class="popup">
      <div class="title justify-content-between">
        <span class="fs26px colorFFF">新增</span>
        <img src="../assets/images/icon-close.png" @click="cancel()" />
      </div>
      <div class="lists" v-if="modelType == 'SBQT'">
        <div class="list">
          <div class="key">设备代号：</div>
          <input
            class="val"
            type="text"
            v-model="equipmentInfo.deviceCode"
            placeholder="请输入设备代号"
          />
        </div>
        <div class="list">
          <div class="key">设备名称：</div>
          <input
            class="val"
            type="text"
            v-model="equipmentInfo.deviceName"
            placeholder="请输入设备名称"
          />
        </div>
        <div class="list">
          <div class="key">单套数量：</div>
          <input
            class="val"
            type="number"
            v-model="equipmentInfo.deviceNum"
            placeholder="请输入单套数量"
          />
          <!-- <div class="unit">个</div> -->
        </div>
        <div class="list">
          <div class="key">设计单位：</div>
          <input
            class="val"
            type="text"
            v-model="equipmentInfo.designOrg"
            placeholder="请输入设计单位"
          />
        </div>
        <div class="list">
          <div class="key">齐套单位：</div>
          <input
            class="val"
            type="text"
            v-model="equipmentInfo.matchOrg"
            placeholder="请输入齐套单位"
          />
        </div>
        <div class="list">
          <div class="key">生产单位：</div>
          <input
            class="val"
            type="text"
            v-model="equipmentInfo.productionOrg"
            placeholder="请输入生产单位"
          />
        </div>
      </div>
      <div
        class="lists"
        v-else-if="
          modelType == 'SBZK' ||
          modelType == 'DJGN' ||
          modelType == 'DXGN' ||
          modelType == 'XTXN'
        "
      >
        <div class="list">
          <div class="key">试验项目：</div>
          <input
            class="val"
            type="text"
            v-model="equipmentInfo.proName"
            placeholder="请输入试验项目"
          />
        </div>
        <!-- <div class="list">
          <div class="key">多媒体示范：</div>
          <input
            class="val"
            type="text"
            v-model="equipmentInfo.mediaId"
            placeholder="请输入多媒体示范"
          />
        </div> -->
      </div>
      <div class="btns justify-content-center">
        <div class="btn sure" @click="save" v-loading="loading">确认</div>
        <div class="btn cancel" @click="cancel()">取消</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "EquipmentAdd",
  props: {
    data: Object,
    taskId: "",
    modelType: "",
  },
  data() {
    return {
      loading: false,
      equipmentInfo: {
        taskId: this.taskId,
        modelType: this.modelType,
        deviceCode: "",
        deviceName: "",
        deviceNum: "",
        designOrg: "",
        matchOrg: "",
        productionOrg: "",
        proName: "",
        // mediaId:"",
      },
    };
  },
  methods: {
    /**
     * 关闭弹窗
     * @param {Number} refresh - 1刷新列表
     */
    cancel(refresh) {
      this.$emit("cancel", refresh);
    },
    // 保存数据
    save() {
      let _this = this;
      if (_this.modelType == "SBQT") {
        if (_this.equipmentInfo.deviceCode == "") {
          return _this.$message({
            message: "设备代号不能为空",
            type: "warning",
          });
        } else if (_this.equipmentInfo.deviceName == "") {
          return _this.$message({
            message: "设备名称不能为空",
            type: "warning",
          });
        } else if (_this.equipmentInfo.deviceNum === "") {
          return _this.$message({
            message: "单套数量不能为空",
            type: "warning",
          });
        } else if (_this.equipmentInfo.deviceNum <= 0) {
          return _this.$message({
            message: "单套数量必须大于0",
            type: "warning",
          });
        }
      } else if (_this.modelType == "SBZK") {
        if (_this.equipmentInfo.proName == "") {
          return _this.$message({
            message: "试验项目不能为空",
            type: "warning",
          });
        }
        // else if (_this.equipmentInfo.mediaId == "") {
        //  return _this.$message({
        //     message: "多媒体示范不能为空",
        //     type: "warning",
        //   });
        // }
      } else if (
        _this.modelType == "DJGN" ||
        _this.modelType == "DXGN" ||
        _this.modelType == "XTXN"
      ) {
        if (_this.equipmentInfo.proName == "") {
          return _this.$message({
            message: "试验项目不能为空",
            type: "warning",
          });
        }
        //  else if (_this.equipmentInfo.mediaId == "") {
        //  return _this.$message({
        //     message: "多媒体示范不能为空",
        //     type: "warning",
        //   });
        // }
      } else {
      }
      let param = {
        list: [_this.equipmentInfo],
      };
      _this.loading = true;
      if (this.modelType == "SBQT") {
        _this.axios
          .saveEquipment(param)
          .then((res) => {
            console.log(res);
            if (res.code == 200) {
              _this.$message({
                message: res.msg,
                type: "success",
              });
              _this.cancel(1);
            } else {
              _this.$message.error(res.msg);
            }
            _this.loading = false;
          })
          .catch(function (error) {
            _this.loading = false;
            _this.$message.error(error.msg);
          });
      } else {
        _this.axios
          .saveEquipmentOther(param)
          .then((res) => {
            console.log(res);
            if (res.code == 200) {
              _this.$message({
                message: res.msg,
                type: "success",
              });
              _this.cancel(1);
            } else {
              _this.$message.error(res.msg);
            }
            _this.loading = false;
          })
          .catch(function (error) {
            _this.loading = false;
            _this.$message.error(error.msg);
          });
      }
    },
  },
};
</script>

<style scoped lang='less'>
.equipment-add {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  .shade {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
  }
  .popup {
    width: 6.39rem;
    // height: 4.9rem;
    padding-bottom: 0.5rem;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 0 0.05rem 0.15rem 0 rgba(0, 21, 54, 0.25);
    .title {
      width: 100%;
      height: 0.58rem;
      background-color: #285fe3;
      padding: 0 0.23rem 0 0.28rem;
      img {
        width: 0.23rem;
        height: 0.23rem;
        cursor: pointer;
      }
    }
    .lists {
      margin-top: 0.57rem;
    }
    .list {
      width: 100%;
      margin-top: 0.24rem;
      padding-left: 0.79rem;
      display: flex;
      align-items: center;
      .key {
        width: 1.37rem;
        font-size: 18px;
        color: #161d2e;
      }
      .val {
        width: 3.2rem;
        height: 0.44rem;
        border: 1px solid #aab3c4;
        font-size: 16px;
        color: #505971;
        padding-left: 0.12rem;
        font-family: "Microsoft YaHei,Arial";
      }
      .val::placeholder {
        color: #aab3c4;
      }
      .val:focus {
        outline: 1px solid #316eff;
        color: #161d2e;
      }
      .unit {
        font-size: 0.18rem;
        color: #161d2e;
        margin-left: 0.06rem;
      }
    }
    .list:first-child {
      margin-top: 0;
    }
    .btns {
      width: 100%;
      margin-top: 0.67rem;
      .btn {
        width: 1.3rem;
        height: 0.4rem;
        margin-left: 0.69rem;
        line-height: 0.4rem;
        text-align: center;
        border-radius: 0.03rem;
        font-size: 0.22rem;
        cursor: pointer;
      }
      .btn.sure {
        background: linear-gradient(0deg, #00a366 0%, #00c37a 100%);
        color: #fff;
      }
      .btn.cancel {
        border: 1px solid #316eff;
        background: linear-gradient(0deg, #d9dde8 0%, #ffffff 100%);
        color: #505971;
      }
      .btn:first-child {
        margin-left: 0;
      }
    }
  }
}
</style>